package com.mobialia.socialmediademo;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.location.Location;
import android.util.Log;

import com.google.ads.AdRequest;
import com.google.ads.AdRequest.Gender;
import com.google.ads.AdView;

public class MyAdManager {
	public static String TAG = "MyAdManager";

	private static AdRequest adRequest;
   
	static {
		adRequest = new AdRequest();
	}

	static void initialize(Context ctx, String gender, String birthday) {
		Map<String, Object> extras = new HashMap<String, Object>();
		String colorbg = ctx.getResources().getString(R.color.actionbar_background).substring(3);
		String colorfg = ctx.getResources().getString(R.color.actionbar_text).substring(3);
		extras.put("color_bg", colorbg);
		extras.put("color_bg_top", colorbg);
		extras.put("color_border", colorbg);
		extras.put("color_link", colorfg);
		extras.put("color_text", colorfg);
		extras.put("color_url", colorfg);

		adRequest.setExtras(extras);
		if ("male".equals(gender)) { 
			adRequest.setGender(Gender.MALE);
		} else if ("female".equals(gender)) {
			adRequest.setGender(Gender.FEMALE);
		}
		if (birthday != null) {
			// from 01/29/1979 to 19790129
			try {
				Log.d(TAG, birthday);
				String birthdayIso = birthday.substring(6, 10) + birthday.substring(0, 2) + birthday.substring(3, 5); 
				Log.d(TAG, birthdayIso);
				adRequest.setBirthday(birthdayIso);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	static void setLocation(Location location) {
		adRequest.setLocation(location);
	}
	
	static void loadAd(AdView adView) {
		adView.loadAd(adRequest);
	}
}
